
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 基于JDK7之后，实现正确关闭流资源方法
 * try - with - resource
 */
public class NewFileCopyTest {
    @Test
    public void copyFile() {
        String originalUrl = "test/NewFileCopyTest.java";
        String targetUrl = "test/new.txt";

        try (
                FileInputStream originalFileInputStream = new FileInputStream(originalUrl);
                FileOutputStream targetFileOutputStream = new FileOutputStream(targetUrl)
        ) {
            int content;
            while ((content = originalFileInputStream.read()) != -1) {
                targetFileOutputStream.write(content);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
